package scct.chenhui.dao;

import scct.chenhui.model.User;
import scct.chenhui.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class UserDao {
    /**
     * @param name 用户名
     * @param pass 密码
     * @return 返回登陆的用户对象
     */
    public User login(String name, String pass) {
        User user = null;
        try {
            Connection connection = DBUtils.getConnection();
            String sql = "select * from sm_user where username=? and password=?";
            PreparedStatement pstmt = connection.prepareStatement(sql);
            pstmt.setString(1, name);
            pstmt.setString(2, pass);
            ResultSet resultSet = pstmt.executeQuery();
            if (resultSet.next()) {
                int id = resultSet.getInt("id");
                String username = resultSet.getString("username");
                String password = resultSet.getString("password");
                int role = resultSet.getInt("role");
                String sex = resultSet.getString("sex");
                String mobile = resultSet.getString("mobile");
                user = new User(username, id, password, role, sex, mobile);

            }
            DBUtils.closeConnection(connection);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return user;
    }

    public List<User> getAllUser() {
        List<User> list = new ArrayList<>();
        try {
            Connection conn = DBUtils.getConnection();
            String sql = "select * from sm_user";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                String username = rs.getString("username");
                String password = rs.getString("password");
                String sex = rs.getString("sex");
                String mobile = rs.getString("mobile");
                int role = rs.getInt("role");
                list.add(new User(username, id, password, role, sex, mobile));

            }
            DBUtils.closeConnection(conn);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


        return list;
    }


    public User getUserById(int id) {
        User user = null;
        try {
            Connection conn = DBUtils.getConnection();
            String sql = "select * from sm_user where id=?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                String username = rs.getString("username");
                String password = rs.getString("password");
                String sex = rs.getString("sex");
                String mobile = rs.getString("mobile");
                int role = rs.getInt("role");
                user = new User(username, id, password, role, sex, mobile);
            }
            DBUtils.closeConnection(conn);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return user;
    }

    public boolean updateUser(User user) {
        int result = 0;
        try {
            Connection conn = DBUtils.getConnection();
            String update_sql = "update sm_user set username=?,password=?,sex=?,mobile=? where id=?";
            PreparedStatement pstmt = conn.prepareStatement(update_sql);
            pstmt.setString(1, user.getUsername());
            pstmt.setString(2, user.getPassword());
            pstmt.setString(3, user.getSex());
            pstmt.setString(4, user.getMobile());
            pstmt.setInt(5, user.getId());
            result = pstmt.executeUpdate();

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return result == 1;
    }

    public boolean isDupName(String name)
    {
        boolean flag=false;
        try {
            Connection conn = DBUtils.getConnection();
            String sql = "select * from sm_user where username=?";
            PreparedStatement pstmt = conn.prepareStatement(sql );
            pstmt.setString(1,name);
            ResultSet rs = pstmt.executeQuery();
            if(rs.next())
            {
                flag=true;
            }
            DBUtils.closeConnection(conn);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return flag;
    }
//    /**
//     * 用户登录
//     * @param name 用户名
//     * @param pass 密码
//     * @param role 用户角色 1 普通用户,2 管理员
//     * @return
//     */
//    public User login(String name, String pass, int role) {
//        User resultUser = null;
//        try {
//            Connection connection = DBUtils.getConnection();
//            String sql = "select * from bm_user where username=? and password=? and role=?";
//            PreparedStatement pstmt = connection.prepareStatement(sql);
//            pstmt.setString(1, name);
//            pstmt.setString(2, pass);
//            pstmt.setInt(3,role);
//            ResultSet resultSet = pstmt.executeQuery();
//            if (resultSet.next()) {
//                resultUser = new User();
//                resultUser.setUsername(resultSet.getString("username"));
//                resultUser.setPassword(resultSet.getString("password"));
//                resultUser.setPhone(resultSet.getString("phone"));
//                resultUser.setSex(resultSet.getString("sex"));
//                resultUser.setId(resultSet.getInt("id"));
//                resultUser.setRole(resultSet.getInt("role"));
//            }
//            DBUtils.closeConnection(connection);
//        } catch (ClassNotFoundException e) {
//            throw new RuntimeException(e);
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//        return resultUser;
//    }
//    /*
//    获取全部用户
//     */
//    public List<User> getAllUsers() {
//        String sql = "select * from bm_user";
//        List<User> userLst = new ArrayList<>();
//        try {
//            Connection connection = DBUtils.getConnection();
//            PreparedStatement pstmt = connection.prepareStatement(sql);
//            ResultSet rs = pstmt.executeQuery();
//            while (rs.next()) {
//                int id = rs.getInt("id");
//                String username = rs.getString("username");
//                String password = rs.getString("password");
//                String phone = rs.getString("phone");
//                String sex = rs.getString("sex");
//                int role = rs.getInt("role");
//                userLst.add(new User(id, username, password, role, sex, phone));
//            }
//            rs.close();
//            DBUtils.closeConnection(connection);
//        } catch (ClassNotFoundException e) {
//            throw new RuntimeException(e);
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//        return userLst;
//    }
//    /**
//     * 根据用户id获取用户对象
//     * @param id  用户id
//     * @return 用户对象
//     */
//    public User getUserById(int id)
//    {
//        String sql="select * from bm_user where id=?";
//        User user=null;
//        try {
//            Connection connection = DBUtils.getConnection();
//            PreparedStatement pstmt = connection.prepareStatement(sql);
//            pstmt.setInt(1,id);
//            ResultSet rs = pstmt.executeQuery();
//            if(rs.next())
//            {
//                int id2 = rs.getInt("id");
//                String username = rs.getString("username");
//                String password = rs.getString("password");
//                String phone = rs.getString("phone");
//                String sex = rs.getString("sex");
//                int role = rs.getInt("role");
//                user = new User(id2, username, password, role, sex, phone);
//            }
//           pstmt.close();
//            DBUtils.closeConnection(connection);
//        } catch (ClassNotFoundException e) {
//            throw new RuntimeException(e);
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//        return user;
//    }
//    /**
//     * 更新用户信息
//     * @param user 更新的用户对象,id字段不变
//     * @return true 更新成功
//     */
//    public boolean updateUser(User user)
//    {
////        依据用户id更新用户信息
//        String sql="update bm_user set username=? ,password=? ,phone=?,sex=? where id=?";
//        int  result;
//        try {
//            Connection connection = DBUtils.getConnection();
//            PreparedStatement pstmt = connection.prepareStatement(sql);
//            pstmt.setString(1,user.getUsername());
//            pstmt.setString(2,user.getPassword());
//            pstmt.setString(3,user.getPhone());
//            pstmt.setString(4,user.getSex());
//            pstmt.setInt(5,user.getId());
//            result = pstmt.executeUpdate();
//            pstmt.close();
//            DBUtils.closeConnection(connection);
//        } catch (ClassNotFoundException e) {
//            throw new RuntimeException(e);
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//        return result==1;
//    }
//
//    /**
//     * 添加用户
//     * @param user 添加的用户信息,无需id值
//     * @return  true 添加成功
//     */
//    public boolean addUser(User user)
//    {
////        insert into bm_user  value (null,"jack","123","1","男","1357777")
//        String insertSql="insert into bm_user  value (null,?,?,1,?,?)";
//        UserDao userDao = new UserDao();
//        int result;
//        try {
//            Connection connection = DBUtils.getConnection();
//            PreparedStatement pstmt = connection.prepareStatement(insertSql);
//            pstmt.setString(1,user.getUsername());
//            pstmt.setString(2,user.getPassword());
//            pstmt.setString(3,user.getSex());
//            pstmt.setString(4,user.getPhone());
//            result = pstmt.executeUpdate();
//
//        } catch (ClassNotFoundException e) {
//            throw new RuntimeException(e);
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }
//      return  result==1;
//    }
//    public boolean isDupName(String name)
//    {
//        String sql="select * from bm_user where username=?";
//        boolean flag=false;
//        try {
//            Connection connection = DBUtils.getConnection();
//            PreparedStatement pstmt = connection.prepareStatement(sql);
//            pstmt.setString(1,name);
//            ResultSet rs = pstmt.executeQuery();
//            if(rs.next())
//            {
//                flag=true;
//            }
//        } catch (ClassNotFoundException e) {
//            throw new RuntimeException(e);
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }
//        return flag;
//    }
//    public  boolean isDupPhone(String phone)
//    {
//        String sql="select * from bm_user where phone=?";
//        boolean flag=false;
//        try {
//            Connection connection = DBUtils.getConnection();
//            PreparedStatement pstmt = connection.prepareStatement(sql);
//            pstmt.setString(1,phone);
//            ResultSet rs = pstmt.executeQuery();
//            if(rs.next())
//            {
//                flag=true;
//            }
//        } catch (ClassNotFoundException e) {
//            throw new RuntimeException(e);
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }
//        return flag;
//    }
}
